package com.sxf.controller.blog;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.WebRequest;

import com.sxf.cache.CacheTool;
import com.sxf.domain.User;
import com.sxf.domain.viewbean.CommentVO;
import com.sxf.domain.viewbean.PostVO;
import com.sxf.domain.viewbean.TermVO;
import com.sxf.domain.viewbean.UserVO;
import com.sxf.querybean.CommentQuery;
import com.sxf.querybean.PostQuery;
import com.sxf.querybean.PostQuery.QueryType;
import com.sxf.querybean.TermQuery;
import com.sxf.querybean.UserQuery;
import com.sxf.service.CommentService;
import com.sxf.service.PostService;
import com.sxf.service.TermService;
import com.sxf.service.UserService;

@Controller
@RequestMapping("blog")
public class BlogIndexController {

	@Resource
	private PostService postService;

	@Resource
	private UserService us;

	@Resource
	private CommentService commentService;

	@Resource
	private TermService termService;

	/**
	 * 默认URL展示方式 http://localhost/wordpress/blog/show?id=1
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/show", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String showDefault(WebRequest request) throws Exception {
		String id = request.getParameter("id");// 用户ID
		String p = request.getParameter("p");// 文章内容
		String author = request.getParameter("author");// 作者
		String cat = request.getParameter("cat");// 分类目录
		String m = request.getParameter("m");// 月度分类
		String paged = request.getParameter("paged");// 分页
		String page_id = request.getParameter("paged_id");// 其他页(模板页)
		String url = null;
		if (StringUtils.isNotBlank(id)) {
			url = blogIndex(request, id, cat, m, paged);
		} else if (StringUtils.isNotBlank(p)) {
			url = content(request, p);
		} else if (StringUtils.isNotBlank(author)) {
			url = blogIndex(request, author, cat, m, paged);
		} else if (StringUtils.isNotBlank(page_id)) {

		}
		request.setAttribute("blogname", CacheTool.getOption(id, "blogname"),
				RequestAttributes.SCOPE_REQUEST);
		request.setAttribute("blogdescription",
				CacheTool.getOption(id, "blogdescription"),
				RequestAttributes.SCOPE_REQUEST);
		return url;
	}

	private String blogIndex(WebRequest request, String id, String cat,
			String m, String paged) throws Exception {

		if (id != null) {
			UserQuery uq = new UserQuery();
			uq.setId(Long.parseLong(id));
			List<UserVO> userList = us.queryList(uq);

			if (userList != null && !userList.isEmpty()) {
				User user = userList.get(0);
				user.setPassword(null);
				request.setAttribute("user", user,
						RequestAttributes.SCOPE_REQUEST);
			}

			PostQuery pq = new PostQuery();
			if (StringUtils.isNotBlank(cat)) {
				pq.queryType = QueryType.queryByTerm;
				pq.setTermId(Long.parseLong(cat.trim()));

				TermQuery tq = new TermQuery();
				tq.setId(Long.parseLong(cat));
				List<TermVO> termList = termService.queryList(tq);
				if (termList != null && !termList.isEmpty()) {
					request.setAttribute("term", termList.get(0),
							RequestAttributes.SCOPE_REQUEST);
				}

			}
			if (StringUtils.isNotBlank(m)) {
				pq.setDateByMonth(m);
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMM");
				request.setAttribute("month", sdf1.parse(m),
						RequestAttributes.SCOPE_REQUEST);
			}

			pq.setAuthor(Long.parseLong(id));
			pq.setType("post");
			pq.setStatus("publish");
			// 每页显示的内容
			int posts_per_page = Integer.parseInt(CacheTool.getOption(id,
					"posts_per_page"));
			pq.setLimit(posts_per_page);
			pq.setQueryAllColumn(true);
			if (StringUtils.isNotBlank(paged)) {
				pq.setOffset(posts_per_page * (Integer.parseInt(paged) - 1));
			}
			List<PostVO> postList = postService.queryList(pq);
			request.setAttribute("article", postList,
					RequestAttributes.SCOPE_REQUEST);

			// 近期评论
			CommentQuery cq = new CommentQuery();
			// cq.setAuthorIP(request.getRemoteAddr());
			cq.setLimit(5);
			List<CommentVO> commentList = commentService.queryList(cq);
			request.setAttribute("commentList", commentList,
					RequestAttributes.SCOPE_REQUEST);

			// 文章归档
			List<Date> archiveList = postService.getArchivePost();
			request.setAttribute("archiveList", archiveList,
					RequestAttributes.SCOPE_REQUEST);

			// 分类目录
			TermQuery tq = new TermQuery();
			tq.setTaxonomy("category");
			List<TermVO> termList = termService.queryList(tq);
			request.setAttribute("termList", termList,
					RequestAttributes.SCOPE_REQUEST);

		}
		// map.put("blogname", CacheTool.getOption("blogname"));
		// map.put("blogdescription", CacheTool.getOption("blogdescription"));

		// mav = new ModelAndView(new RedirectView(""), map);
		// mav.setView(new RedirectView(""));//重定向
		return "blog/index";
	}

	private String content(WebRequest request, String p) throws Exception {
		do {
			if (StringUtils.isNotBlank(p)) {
				long postId = Long.parseLong(p);
				PostQuery postQuery = new PostQuery();
				postQuery.setId(postId);
				postQuery.setQueryAllColumn(true);
				List<PostVO> postList = postService.queryList(postQuery);
				if (postList != null && !postList.isEmpty()) {
					request.setAttribute("post", postList.get(0),
							WebRequest.SCOPE_REQUEST);
				} else {
					break;
				}
				
				// 下一条记录
				postQuery = new PostQuery();
				postQuery.setId(postId);
				postQuery.setQueryAllColumn(false);
				postQuery.setNear(1);
				PostVO nextPost = postService.queryOne(postQuery);
				request.setAttribute("nextPost", nextPost,
						WebRequest.SCOPE_REQUEST);

				// 上一条记录
				postQuery = new PostQuery();
				postQuery.setId(postId);
				postQuery.setQueryAllColumn(false);
				postQuery.setNear(-1);
				PostVO prePost = postService.queryOne(postQuery);
				request.setAttribute("prePost", prePost,
						WebRequest.SCOPE_REQUEST);

				UserQuery uq = new UserQuery();
				uq.setId(postList.get(0).getAuthor());
				List<UserVO> userList = us.queryList(uq);
				if (userList != null && !userList.isEmpty()) {
					request.setAttribute("user", userList.get(0),
							WebRequest.SCOPE_REQUEST);
				}

				int comments_per_page = Integer.parseInt(CacheTool.getOption(
						String.valueOf(postList.get(0).getAuthor()),
						"comments_per_page"));
				CommentQuery cq = new CommentQuery();
				cq.setPostId(postList.get(0).getId());
				// cq.setAuthorIP(request.getRemoteAddr());
				cq.setLimit(comments_per_page);
				Map<Long, CommentVO> commentMap = commentService
						.queryListComment(cq);
				request.setAttribute("commentMap", commentMap,
						WebRequest.SCOPE_REQUEST);
			}
		} while (false);
		//return "blog/content/content";
		return "blog/content";
	}
}
